<template>
  <div>
    <el-form :inline="true" :model="dataForm" ref="dataForm" @keyup.enter.native="getDataList()" size="mini" label-width="150px">
      <#list fileds as param>
      <el-form-item label="${param.columnComment}：">
        <el-input v-model="dataForm.${param.javaColumnName}" placeholder="${param.columnComment}" clearable></el-input>
      </el-form-item>
      </#list>
    </el-form>
    <div style="text-align: center">
      <el-button @click="getDataList()" size="mini" type="primary">查询</el-button>
      <el-button @click="reset()" size="mini">重置</el-button>
    </div>
    <hr/>
    <div style="margin-bottom: 5px">
      <el-button v-if="isAuth('project:dictionary:save')" type="primary" size="mini" @click="addOrUpdateHandle(null)">新增</el-button>
      <el-button v-if="isAuth('project:dictionary:delete')" type="danger"  size="mini" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
    </div>

    <el-table :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle" style="width: 100%;border-radius: 5px" size="mini" class="width-bg"
               border tooltip-effect="dark" :header-cell-style="{background:'#eef1f6',color:'#606266'}">
      <el-table-column type="selection" header-align="center" align="center" width="50">
      </el-table-column>
      <#list fileds as param>
      <el-table-column prop="${param.javaColumnName}" header-align="center" align="center" label="${param.columnComment}">
      </el-table-column>
      </#list>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button v-if="isAuth('project:dictionary:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button v-if="isAuth('project:dictionary:delete')" type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
        <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './${beanName?uncap_first}-add-or-update'
  import { ${beanName?uncap_first}Page , ${beanName?uncap_first}Delete } from '@/api/${beanName?uncap_first}'
  export default {
    data () {
      return {
        dataForm: {
        <#list fileds as param>
          ${param.javaColumnName}:null,
        </#list>
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
          const param= {
            'current': this.pageIndex,
            'size': this.pageSize,
            'param': this.dataForm
          }
          const requestData={
            'requestData':param
          }
          ${beanName?uncap_first}Page(requestData).then((res)=>{
            this.dataListLoading = false
            if(res.code=='0000'){
              this.dataList = res.responseData.records
              this.totalPage = res.responseData.total
            }else{
              this.dataList = []
              this.totalPage = 0
            }
          }).catch((err)=>{

          })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
    // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
            this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var userIds = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm("确定删除", '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const requestData={
            'requestData':userIds
          }
        ${beanName?uncap_first}Delete(requestData).then((res)=>{
          if(res.code=='0000'){
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          }
        }).catch((err)=>{
            this.$message.error(err)
        })
        }).catch(() => {})
      },
      reset(){
        <#list fileds as param>
        this.dataForm.${param.javaColumnName}=null
        </#list>
        this.getDataList()
      }
    }
 }
</script>
